import prompt from '@ohos.prompt';
import router from '@ohos.router';
import ajax from '../../http/http';

export default {
    data: {
        rooms: [],
        num:0,
        str:''
    },
    onInit() {
        this.initData();
    },
    initData() {
        let param = {
            "boxGroup": "smart_home"
        };
        ajax.post('/experiment/device/detail/all', param, (result) => {
            this.num = result.data.length;
            // 开始解析服务端返回的数据
            for (let i = 0;i < result.data.length; i++) {
                let data = result.data[i];
                // 优先构造基本的家居业务数据，如：名称、设备数、场景背景图等
                let room = {
                    id: data.id,
                    name: data.boxName,
                    equipmentNum: data.devices.length,
                    boxImage:this.$app.$def.globalData.imageUrl + data.boxImage,
//                    boxImage:"common/images/home3.png",
                    devices: data.devices,
                    luminance: '--',
                    temperature: '--',
                    humidity: '--',
                    pm25: '--'
                };
                // 如果家居场景下有设备，解析出这些设备的影子数据用于首页渲染
                if (data.devices.length != 0) {
                    let name = data.boxName;
                    let deviceShadow = data.deviceShadow;
                    for (let j = 0;j < deviceShadow.length; j++) {
                        let properties = deviceShadow[j].shadow[0].reported.properties;
                        if (properties.Luminance != undefined) {
                            room.luminance = properties.Luminance;
                        }
                        if (properties.Temperature != undefined) {
                            room.temperature = properties.Temperature;
                        }
                        if (properties.Humidity != undefined) {
                            room.humidity = properties.Humidity;
                        }
                        if (properties.PM25 != undefined) {
                            room.pm25 = properties.PM25;
                        }
                    }
                }
                this.rooms.push(room);
            }
        });
    },
    queryEquipment(id) {
        let data = {};
        for (let i in this.rooms) {
            if (id == this.rooms[i].id) {
                data = this.rooms[i];
            }
        }
        router.push({
            url: 'pages/example/example',
            params: {
                data
            }
        })
    },
    addEquipment() {
        router.push({
            url: 'pages/addEquipment/addEquipment'
        })
    },
    toFamilyRoom() {
        router.push({
            url: 'pages/familyRoom/familyRoom'
        })
    }
}
